<template>
	<view class="apply">
		<cu-custom bgColor="bg-gradual-orange" :isBack="true">
			<block slot="content">修改资料</block>
		</cu-custom>
    
		<view class="cu-form-group">
			<view class="title">商家简称</view>
			<input v-model="form.shortname" :disabled="disabled" placeholder="10个字以内" maxlength="10"></input>
		</view>
		<!-- <view class="cate-choose cu-form-group no-border">
			<view class="title">入驻类型</view>
			<input v-model="type.name" @tap="changeType" disabled="disabled" placeholder="请选择入驻分类"></input>
		</view> -->
		<view class="cu-form-group">
			<view class="title">手机号码</view>
			<input v-model="form.mobile" :disabled="disabled" placeholder="请输入手机号码" maxlength="11"></input>
		</view>
		<!-- #ifndef H5 || APP-PLUS || MP-ALIPAY -->
		<view class="cu-form-group">
			<view class="title">所在地区</view>
			<picker mode="region" @change="RegionChange" :value="form.region" :disabled="disabled">
				<view class="picker">
					{{form.region[0]}}，{{form.region[1]}}，{{form.region[2]}}
				</view>
			</picker>
		</view>
		<view class="group-bg"></view>
		<view class="cu-form-group" v-if="typeChoose">
			<view class="title">折扣比例</view>
			<input v-model="form.discount" placeholder="例：7折输入70" maxlength="10"></input>
			<text class='text-black'>%</text>
		</view>
		<!-- #endif -->
    
		<view class="cu-form-group">
			<view class="title">地址选择</view>
			<input v-model="form.address" :disabled="true" @tap="ChooseLocation"></input>
			<text class='cuIcon-locationfill text-orange'></text>
		</view>
    
    <view class="title img-title"><text class="title-text">店铺照片</text></view>
		<view class="cu-form-group">
			<view class="file">
				<my-img-upload-item label="店铺照" v-model="form.store_entrance_pic"></my-img-upload-item>
				<my-img-upload-item label="环境照" v-model="form.indoor_pic"></my-img-upload-item>
			</view>
		</view>

    <view class="title img-title"><text class="title-text">店铺相册</text></view>
		<view class="cu-bar bg-white">
			<view class="action">
				{{form.imgList.length}}/8
			</view>
		</view>
		<my-img-upload-group num="8" v-model="form.imgList"></my-img-upload-group>

		<view class="padding flex flex-direction">
			<button class="cu-btn bg-gradual-orange margin-tb-sm lg" :disabled="disabled" :loading="loading" @tap="submit">提交</button>
		</view>
	</view>
</template>

<script>
	import { upload } from "@/common/upload.js";
	import shop from '@/api/shop';
	import MyImgUploadItem from '@/components/MyImgUploadItem.vue';
	import MyImgUploadGroup from '@/components/MyImgUploadGroup.vue';
	export default {
		data() {
			return {
				form: {
					shortname: '',
					region: ['北京市', '北京市', '东城区'],
					lat: '',
					lng: '',
					address: '',
					mobile:'',
					discount:'',
					store_entrance_pic: '',
					indoor_pic: '',
					rebate:'',
					imgList: [],
				},
				info: {},
				type:{name:'请选择入驻分类'},
				typeList:[],
				itemList:[],
				typeChoose:true,
				disabled: false,
				loading: false,
			}
		},
		components: {
			MyImgUploadGroup,
			MyImgUploadItem
		},
		onLoad() {
			this.loadInfo();
		},
		computed:{},
		methods: {
			changeType(){
				if(this.itemList.length<1){
					return false;
				}
				let itemList = this.itemList;
				let _this = this;
				uni.showActionSheet({
					itemList: itemList,
					success: function (res) {
						// console.log('选中了第' + (res.tapIndex + 1) + '个按钮');
						if(_this.info.shop_type[res.tapIndex].name=='智慧收银'){
							_this.showModels(res.tapIndex);
						}else{
							_this.type = _this.info.shop_type[res.tapIndex];
							_this.form.rebate = _this.info.shop_type[res.tapIndex].id;
							_this.typeChoose = true;
						}
					},
					fail: function (res) {
					}
				});
			},
			showModels(index){
				let _this = this;
				uni.showModal({
					title:'提示',
					content:'确定要关闭智慧经营模式吗?',
					success:function(res){
						if(res.confirm){
							_this.type = _this.info.shop_type[index];
							_this.form.rebate = _this.info.shop_type[index].id;
							_this.typeChoose = false;
						}else{
							return false;
						}
					}
				})
			},
			async loadInfo() {
				shop.profile().then(res => {
					if (res.code === 1) {
						this.info = res.data;
						if (this.info) {
							this.form.shortname = this.info.shortname;
							this.form.discount = this.info.discount;
							this.form.rebate = this.info.rebate;
							if (this.info.region) {
								this.form.region = this.info.region.split(',');
							}
							if (this.info.mobile) {
								this.form.mobile = this.info.mobile.split(',');
							}
							this.form.address = this.info.address;
							if (this.info.lat && this.info.lng) {
								this.form.lat = this.info.lat;
								this.form.lng = this.info.lng;
							}
							this.form.store_entrance_pic = this.info.store_entrance_pic;
							this.form.indoor_pic = this.info.indoor_pic;
							if (this.info.imgList) {
								this.form.imgList = this.info.imgList.split(',');
							}
							if(this.info.shop_type){
								let itemList = [];
								for (let i = 0;i<this.info.shop_type.length;i++) {
									itemList.push(this.info.shop_type[i].name);
									if(this.info.shop_type[i].id == this.info.rebate){
										this.type = this.info.shop_type[i];
										if(this.info.shop_type[i].name=='智慧收银'){
											this.typeChoose=false;
										}
									}
								}
								this.itemList = itemList;
							}
							
						}
					} else {
						this.toast(res.msg);
					}
				})
			},
			async submit() {
				if (this.loading) {
					return;
				}
				this.loading = true;
				setTimeout(() => {
					this.loading = false
				}, 7000)

				shop.profileUpdate(this.form).then(res => {
					this.loading = false;
					this.toast(res.msg);
				})
			},
			ChooseLocation() {
				if (this.loading || this.disabled) {
					return;
				}
				uni.chooseLocation({
					latitude: this.form.lat > 0 ? this.form.lat : '',
					longitude: this.form.lng > 0 ? this.form.lng : '',
					success: (res) => {
						// console.log(res)
						if (!res.name) {
							this.form.address = res.address + res.name
							this.form.lat = res.latitude
							this.form.lng = res.longitude
							this.toast('请选择详细位置');
						} else {
							this.form.address = res.name
							this.form.lat = res.latitude
							this.form.lng = res.longitude
						}
					}
				});
			},
			RegionChange(e) {
				if (this.loading || this.disabled) {
					return;
				}
				this.form.region = e.detail.value
			},
		}
	}
</script>

<style lang="less">
	.apply {
    background: #fff;
		.cu-btn[disabled] {
			color: #FFFFFF !important;
		}
	}

	.tips {
		background-color: #FFFFFF;
		color: red;
		margin-top: -10px;
		padding: 5px 15px;
		border-bottom: 0.5px solid #eee;
	}

  .cu-form-group{
    border-bottom: 1px solid #D8D8D8;
    margin: 0 10px;
    background: #fff;
  }
	.cu-form-group .title {
		min-width: calc(4em + 15px);
	}
  
  .img-title{
    padding: 15px;
    margin: 0 10px;
    background: #fff;
    border-bottom: 1px solid #D8D8D8;
    
    .title-text{
      background: #DBDBDB;
      padding: 4px 10px;
      border-radius: 4px;
    }
  }

	.cu-form-group {
		.file {
			flex: 1;

			.item {
				display: inline-block;
				padding: 10px 20px 0 0;
				position: relative;

				.cuIcon-add {
					box-sizing: border-box;
					display: block;
					width: 60px;
					height: 60px;
					line-height: 60px;
					text-align: center;
					border: 1px dashed #DDDDDD;
					font-size: 30px;
					color: #aaa;
				}

				.cuIcon-close {
					position: absolute;
					right: 20px;
					top: 10px;
					width: 18px;
					height: 18px;
					line-height: 18px;
					border-radius: 2px;
					text-align: center;
					background-color: #00000060;
					color: #FFFFFF;
					z-index: 999;
				}

				.img {
					display: block;
					width: 60px;
					height: 60px;
				}

				.label {
					display: block;
					line-height: 25px;
					font-size: 12px;
					text-align: center;
					color: #aaa;
				}
			}
		}
	}
</style>
